// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;
import { Constant } from "../constant/Constant";
@ccclass
export default class FixScreen extends cc.Component {

    @property(cc.Node)
    left: cc.Node = null;
    @property(cc.Node)
    right: cc.Node = null;

    @property
    speed: number = 0.5;

    /**
     * 自动组合两个方块
     */
    public autoFixed(): void {
        this.stopAnimation();
        let R_M = cc.moveTo(this.speed, new cc.Vec2(this.right.width / 2, this.right.y));
        let L_M = cc.moveTo(this.speed, new cc.Vec2(- this.left.width / 2, this.right.y));
        let spaw_R = cc.spawn(R_M, cc.rotateTo(this.speed, 0, 360));
        let spaw_L = cc.spawn(L_M, cc.rotateTo(this.speed, 0, -360));
        this.right.runAction(spaw_R);
        this.left.runAction(spaw_L);
    }
    /**
     * 方块总体左移
     */
    public allToLeft() {
        this.stopAnimation();
        let LtoLeft = cc.moveTo(this.speed, new cc.Vec2(-(Constant.SCREEN.width - this.right.width) / 2, this.right.y));
        let RtoLeft = cc.moveTo(this.speed, new cc.Vec2(-(Constant.SCREEN.width / 2 - (this.left.width + this.left.width / 2)), this.left.y));
        let spaw_R = cc.spawn(RtoLeft, cc.rotateTo(this.speed, 0, 180));
        let spaw_L = cc.spawn(LtoLeft, cc.rotateTo(this.speed, 0, -180));
        this.right.runAction(spaw_R);
        this.left.runAction(spaw_L);
    }
    /**
     * 方块总体右移
     */
    public allToRight() {
        this.stopAnimation();
        // let fixScreen: FixScreen = cc.find("Canvas/GameObject").getComponent(FixScreen);
        let RtoRgiht = cc.moveTo(this.speed, new cc.Vec2((Constant.SCREEN.width - this.left.width) / 2, this.left.y));
        let LtoRight = cc.moveTo(this.speed, new cc.Vec2((Constant.SCREEN.width / 2 - (this.right.width + this.left.width / 2)), this.left.y));
        let spaw_R = cc.spawn(RtoRgiht, cc.rotateTo(this.speed, 0, 180));
        let spaw_L = cc.spawn(LtoRight, cc.rotateTo(this.speed, 0, -180));
        this.right.runAction(spaw_R);
        this.left.runAction(spaw_L);
    }

    /**
     * 分开移动
     */
    public splitOver() {
        this.stopAnimation();
        let R_R = cc.moveTo(this.speed, new cc.Vec2((Constant.SCREEN.width - this.right.width) / 2, this.right.y));
        let L_L = cc.moveTo(this.speed, new cc.Vec2(-(Constant.SCREEN.width - this.left.width) / 2, this.left.y));
        let spaw_R = cc.spawn(R_R, cc.rotateTo(this.speed, 0, 180));
        let spaw_L = cc.spawn(L_L, cc.rotateTo(this.speed, 0, -180));
        this.right.runAction(spaw_R);
        this.left.runAction(spaw_L);
    }
    /**
     * 停止所有进行的动画
     */
    private stopAnimation() {
        this.left.stopAllActions();
        this.right.stopAllActions();
    }
    start() {
        let width = this.left.width = Constant.SCREEN.width / 7;
        this.left.height = width;
        this.right.width = width;
        this.right.height = width;
        this.right.getComponent(cc.BoxCollider).size.width = width * 0.8;
        this.left.getComponent(cc.BoxCollider).size.width = width * 0.8;
        this.autoFixed();
    }

    // update (dt) {}
}
